package com.zhang.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.Map;

import com.mysql.cj.util.StringUtils;
import com.zhang.gulimall.product.entity.SkuImagesEntity;
import com.zhang.gulimall.product.entity.SpuInfoDescEntity;
import com.zhang.gulimall.product.service.*;
import com.zhang.gulimall.product.vo.SkuItemVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.gulimall.common.utils.PageUtils;
import com.zhang.gulimall.common.utils.Query;

import com.zhang.gulimall.product.dao.SkuInfoDao;
import com.zhang.gulimall.product.entity.SkuInfoEntity;

import javax.annotation.Resource;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Resource
    SpuInfoDescService spuInfoDescService;

    @Resource
    AttrGroupService attrGroupService;

    @Resource
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Resource
    SkuImagesService skuImagesService;

    @Resource
    ThreadPoolExecutor executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByParams(Map<String, Object> params) {

        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        String catelogId = (String) params.get("catelogId");
        String brandId = (String) params.get("brandId");
        String key = (String) params.get("key");
        int min = Integer.parseInt((String) params.get("min"));
        int max = Integer.parseInt((String) params.get("max"));
        if (!StringUtils.isEmptyOrWhitespaceOnly(catelogId) && !catelogId.equals("0")) {
            wrapper.lambda().eq(SkuInfoEntity::getCatalogId, catelogId);
        }
        if (!StringUtils.isEmptyOrWhitespaceOnly(brandId) && !brandId.equals("0")) {
            wrapper.lambda().eq(SkuInfoEntity::getBrandId, brandId);
        }
        if (min >= 0 && max != 0 && max > min) {
            wrapper.lambda().ge(SkuInfoEntity::getPrice, min);
            wrapper.lambda().le(SkuInfoEntity::getPrice, max);
        }
        if (!StringUtils.isEmptyOrWhitespaceOnly(key)) {
            wrapper.lambda().like(SkuInfoEntity::getSkuName, key);
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public SkuItemVo item(String skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();

        //1.sku基本信息
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfo = getById(skuId);
            skuItemVo.setSkuInfo(skuInfo);
            return skuInfo;
        }, executor);

        //2.sku图片信息
        CompletableFuture<Void> imgFuture = CompletableFuture.runAsync(() -> {
            QueryWrapper<SkuImagesEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SkuImagesEntity::getSkuId, skuId);
            List<SkuImagesEntity> list = skuImagesService.list(queryWrapper);
            skuItemVo.setSkuImg(list.stream().map(SkuImagesEntity::getImgUrl).collect(Collectors.toList()));
        }, executor);

        //3.spu销售属性组合
        CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync(res -> {
            List<SkuItemVo.SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrsBySpuId(res.getSpuId());
            skuItemVo.setSkuSaleAttr(saleAttrVos);
        }, executor);

        //4.spu介绍
        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync(res -> {
            SpuInfoDescEntity spuInfoDesc = spuInfoDescService.getById(res.getSpuId());
            skuItemVo.setSpuInfoDesc(spuInfoDesc);
        });

        //5.spu规格参数
        CompletableFuture<Void> attrGroupFuture = infoFuture.thenAcceptAsync(res -> {
            List<SkuItemVo.SpuItemAttrGroup> attrGroups = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            skuItemVo.setAttrGroups(attrGroups);
        });

        CompletableFuture.allOf(infoFuture, imgFuture, saleAttrFuture, descFuture, attrGroupFuture).get();

        return skuItemVo;

//        SkuItemVo skuItemVo = new SkuItemVo();
//        SkuInfoEntity skuInfo = getById(skuId);
//        String spuId = skuInfo.getSpuId();
//        String catalogId = skuInfo.getCatalogId();
//
//        //1.sku基本信息
//        skuItemVo.setSkuInfo(skuInfo);
//
//        //2.sku图片信息
//        QueryWrapper<SkuImagesEntity> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().eq(SkuImagesEntity::getSkuId, skuId);
//        List<SkuImagesEntity> list = skuImagesService.list(queryWrapper);
//        skuItemVo.setSkuImg(list.stream().map(SkuImagesEntity::getImgUrl).collect(Collectors.toList()));
//
//        //3.spu销售属性组合
//        List<SkuItemVo.SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrsBySpuId(spuId);
//        skuItemVo.setSkuSaleAttr(saleAttrVos);
//
//        //4.spu介绍
//        SpuInfoDescEntity spuInfoDesc = spuInfoDescService.getById(spuId);
//        skuItemVo.setSpuInfoDesc(spuInfoDesc);
//
//        //5.spu规格参数
//        List<SkuItemVo.SpuItemAttrGroup> attrGroups = attrGroupService.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
//        skuItemVo.setAttrGroups(attrGroups);
//
//        return skuItemVo;
    }

    @Override
    public List<SkuInfoEntity> listBySkuIds(List<String> ids) {
        return this.baseMapper.selectBatchIds(ids);
    }
}